/*
 * constGridDialog.java
 *
 * Created on August 12, 2004, 2:04 PM
 */

/**
 *
 * @author  pablo
 */

package edu.umd.essic.maskbuilder;

import java.awt.*;

public class constGridDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    private float Nlon, Nlat; // degrees
    private float grid_resolution = 1;
    Point Nodes;
    
    /** Creates new form constGridDialog */
    public constGridDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        Nodes = new Point(0,0);
        initComponents();
        getRootPane().setDefaultButton(jButton1);
        //super;
    }
    
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public Point getReturnStatus() {
        if(returnStatus == RET_OK){
        return Nodes;
        }else{return null;}
    }
    
    private boolean CheckValues(){
        boolean answer = true;
        int m,n;
        try {
            m = Integer.parseInt(NLonText.getText());
            n = Integer.parseInt(NLatText.getText());
        } catch (NumberFormatException e){ return false;}
        if ((m<=0)||(n<=0))
            answer = false;
        return answer;
    }
    
    public void setNodes(float nlon, float nlat){
        Nlon = nlon;
        Nlat = nlat;
        int nx = (int)(Nlon/grid_resolution)+1;
        int ny = (int)(Nlat/grid_resolution)+1;
        NLonText.setText(String.valueOf(nx));
        NLatText.setText(String.valueOf(ny));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        ResolutionTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        NLonText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        NLatText = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridLayout(3, 1, 2, 1));

        setTitle("Grid size definition");
        setModal(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout(1, 3, 0, 5));

        jPanel2.setBorder(new javax.swing.border.TitledBorder("Enter desired resolution"));
        jPanel2.setPreferredSize(new java.awt.Dimension(300, 50));
        jLabel2.setText(" Delta");
        jPanel2.add(jLabel2);

        ResolutionTextField.setText("1");
        ResolutionTextField.setPreferredSize(new java.awt.Dimension(75, 27));
        jPanel2.add(ResolutionTextField);

        jLabel4.setText(" (degrees)");
        jPanel2.add(jLabel4);

        jButton1.setText("Apply");
        jButton1.setActionCommand("Accept!");
        jButton1.setPreferredSize(new java.awt.Dimension(75, 27));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton1);

        getContentPane().add(jPanel2);

        jPanel1.setLayout(new java.awt.GridLayout(1, 0, 1, 5));

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Edit number of nodes if necessary"));
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 50));
        jLabel1.setText(" Nodes on X");
        jPanel1.add(jLabel1);

        NLonText.setNextFocusableComponent(NLatText);
        NLonText.setPreferredSize(new java.awt.Dimension(75, 27));
        jPanel1.add(NLonText);

        jLabel3.setText(" Nodes on Y");
        jPanel1.add(jLabel3);

        NLatText.setNextFocusableComponent(okButton);
        NLatText.setPreferredSize(new java.awt.Dimension(75, 27));
        jPanel1.add(NLatText);

        getContentPane().add(jPanel1);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 7));

        jPanel3.setPreferredSize(new java.awt.Dimension(300, 50));
        okButton.setText("OK");
        okButton.setName("OkButton");
        okButton.setNextFocusableComponent(cancelButton);
        okButton.setPreferredSize(new java.awt.Dimension(75, 27));
        okButton.setSelected(true);
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jPanel3.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel3.add(cancelButton);

        getContentPane().add(jPanel3);

        pack();
    }//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        getRootPane().setDefaultButton(jButton1);
    }//GEN-LAST:event_formComponentShown

    private boolean CheckValue(){
        boolean answer = true;
        try {
            grid_resolution = Float.parseFloat(ResolutionTextField.getText());
        } catch (NumberFormatException e){return false;}  
        if ((grid_resolution<=0)||(grid_resolution>Nlon)||(grid_resolution>Nlat))
            answer = false;
        return answer;
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!CheckValue()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            int nx = (int)(Nlon/grid_resolution)+1;
            int ny = (int)(Nlat/grid_resolution)+1;
            NLonText.setText(String.valueOf(nx));
            NLatText.setText(String.valueOf(ny));
            getRootPane().setDefaultButton(okButton);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (!CheckValues()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {      
            Nodes.setLocation(Integer.parseInt(NLonText.getText()), Integer.parseInt(NLatText.getText()));
            doClose(RET_OK);
        }
    }//GEN-LAST:event_okButtonActionPerformed
        
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
    
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NLatText;
    private javax.swing.JTextField NLonText;
    private javax.swing.JTextField ResolutionTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    
    private int returnStatus = RET_CANCEL;
}
